package com.glorysoft.sunnypda.ui.eqpmovein

import android.app.Activity
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.FabEntity
import com.glorysoft.sunnypda.entity.LineEntity
import com.glorysoft.sunnypda.entity.PositionEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.model.EqpRepository
import com.glorysoft.sunnypda.ui.eqpregister.EqpRegisterViewModel
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: EqpMoveInViewModel
 * Author: XJH
 * Date: 2022/1/11 15:48
 * Description:
 * History:
 */
class EqpMoveInViewModel : BaseViewModel() {


    val onClick: EqpRegisterViewModel.DeleteEqp = object : EqpRegisterViewModel.DeleteEqp {
        override fun onDelete(item: EqpInfoEntity) {
            eqpItems.remove(item)
        }

        override fun onClick(item: EqpInfoEntity) {
            val bundle = Bundle()
            bundle.putSerializable("DATA", item)
            bundle.putInt("TYPE", 1)
            defUI.startActivity.value = bundle
        }

    }

    var eqpItems = ObservableArrayList<EqpInfoEntity>()
    var eqpBinding =
        ItemBinding.of<EqpInfoEntity>(BR.eqpItem, R.layout.item_add_eqp_register)
            .bindExtra(BR.onDeleteClick, onClick)

    val eqpRepository: EqpRepository = EqpRepository()
    var fabList = arrayListOf<String>()
    var lineList = arrayListOf<String>()
    var posList = arrayListOf<String>()
    var entity: EqpInfoEntity? = null
    var fabEntityList: ArrayList<FabEntity> = arrayListOf()
    var lineEntityList: ArrayList<LineEntity> = arrayListOf()
    var posEntityList: ArrayList<PositionEntity> = arrayListOf()

    fun getFabList() {
        val map = JsonObject()
        launchOnlyResult({
            val allFabLinePosition = eqpRepository.getAllFabLinePosition(map)
            fabEntityList.clear()
            fabEntityList.addAll(allFabLinePosition.FABLIST)
            fabList.add("")
            for (areaInfoEntity in allFabLinePosition.FABLIST) {
                fabList.add(areaInfoEntity.FABID+":"+areaInfoEntity.FAB)
            }
        })
    }

    fun getLineList(fab: String) {
        lineEntityList.clear()
        lineList.clear()
        posList.clear()
        fabEntityList.forEach {
            if (it.FAB == fab) {
                lineEntityList.addAll(it.LINELIST)
            }
        }
        lineList.add("")
        lineEntityList.forEach {
            lineList.add(it.LINEID+":"+it.LINE)
        }
    }

    fun getPositionList(line: String) {
        posList.clear()
        posEntityList.clear()
        posList.add("")
        lineEntityList.forEach {
            if (it.LINE == line) {
                it.POSITIONLIST.forEach {
                    posList.add(it.POSITIONID+":"+it.POSITION)
                }
            }
            posEntityList.addAll(it.POSITIONLIST)
        }
    }

    fun getEqpInfoMovein(codeS: String) {
        val map = JsonObject()
        map.addProperty("EQPID", codeS)
        launchOnlyResult({
            entity = eqpRepository.getEqpInfoMovein(map)
        })

    }

    fun addEqpToList(fab: String, line: String, pos: String) {
        var mFabID = if (fab == "")"" else fab.split(":")[0]
        var mLineID = if (line == "")"" else line.split(":")[0]
        var mPosID = if (pos == "")"" else pos.split(":")[0]

        eqpItems.forEach {
            if (it.EQPID == entity?.EQPID) {
                defUI.toastEvent.value = ToastMessageBean("当前设备已添加", false)
                return
            }
            if (pos != "") {
                if (it.FABID == mFabID && it.LINEID == mLineID && it.POSITIONID == mPosID) {
                    defUI.toastEvent.value = ToastMessageBean("当前位置设备已添加", false)
                    return
                }
            }
        }

        entity?.NO = (eqpItems.size + 1).toString()
        fabEntityList.forEach {
            if (mFabID == it.FABID) {
                entity?.FAB = it.FAB
                entity?.FABID = it.FABID
            }
        }
        lineEntityList.forEach {
            if (mLineID == it.LINEID) {
                entity?.LINE = it.LINE
                entity?.LINEID = it.LINEID
            }
        }
        posEntityList.forEach {
            if (mPosID == it.POSITIONID) {
                entity?.POSITION = it.POSITION
                entity?.POSITIONID = it.POSITIONID
            }
        }

        val map = JsonObject()
        map.addProperty("EQPID",entity?.EQPID)
        map.addProperty("FAB", if (fab == "")"" else fab.split(":")[1])
        map.addProperty("LINE", if (line == "")"" else line.split(":")[1])
        map.addProperty("POSITION", if (pos == "")"" else pos.split(":")[1])
        map.addProperty("FABID", if (fab == "")"" else fab.split(":")[0])
        map.addProperty("LINEID", if (line == "")"" else line.split(":")[0])
        map.addProperty("POSITIONID", if (pos == "")"" else pos.split(":")[0])
        launchOnlyResult({
            eqpRepository.isFabLinePosition(map)
            defUI.callObserve.call()
        })
    }

    fun submit(context: Activity, bitmap: String) {
        val map = JsonObject()
        map.add("BodyEQPList", JsonParser().parse(Gson().toJson(eqpItems)).asJsonArray)
        map.addProperty("CONFIRMOP", bitmap)
        launchOnlyResult({
            eqpRepository.getEqpMoveIn(map)
            defUI.toastEvent.value = ToastMessageBean(context.getString(R.string.success), true)
            context.finish()
        })

    }
}